import axios from 'axios'
import { Notification, Message } from '@arco-design/web-react';

export const request = (config) => {
    const http = axios.create({
        baseURL: '/api'
    })

    //      请求拦截
    http.interceptors.request.use((config) => {
        const token = localStorage.getItem('token');
        config.headers = {
            Authorization: 'Bearer ' + token,
        };
        return config
    }, (error) => {
        console.error('error-request', error.response)

    })

    // 响应拦截
    http.interceptors.response.use((res) => {
        return res.data || res
    }, (error) => {
        console.error('error-response', error.response)
        const response = error.response;
        if (response && response.status) {
            if (response.status === 403) {

                Message.error(response.data.msg)
                setTimeout(() => {
                    location.href = '/blog/welcome';
                }, 1500)
            }
            if (response.status === 401) {
                Message.error('token过期，请重新登录')
                setTimeout(() => {
                    location.href = '/login';
                }, 1500)
            }
        }

    })

    return http(config)
}


export interface responseInterFace {
    code: number,
    data: Object,
    msg: string
}